package org.example;

import org.example.config.AlipayConfig;
import org.example.model.dto.MerchantOnboardingRequest;
import org.example.model.dto.MerchantOnboardingResponse;
import org.example.model.dto.QueryOnboardingStatusRequest;
import org.example.service.ImageUploadService;
import org.example.service.MerchantOnboardingService;

/**
 * 支付宝商家进件示例程序
 */
public class App {
    public static void main(String[] args) {
        System.out.println("=== 支付宝商家进件系统 ===\n");

        // 1. 配置支付宝参数（请替换为您的真实配置）
        String appId = "YOUR_APP_ID";  // 应用ID
        String privateKey = "YOUR_PRIVATE_KEY";  // 应用私钥
        String alipayPublicKey = "YOUR_ALIPAY_PUBLIC_KEY";  // 支付宝公钥
        boolean useSandbox = true;  // 是否使用沙箱环境

        // 创建配置对象
        AlipayConfig config = new AlipayConfig(appId, privateKey, alipayPublicKey, useSandbox);

        // 2. 创建服务实例
        MerchantOnboardingService onboardingService = new MerchantOnboardingService(config);
        ImageUploadService imageUploadService = new ImageUploadService(config);

        // 3. 演示完整的进件流程
        demonstrateOnboardingFlow(onboardingService, imageUploadService);
    }

    /**
     * 演示完整的商家进件流程
     */
    private static void demonstrateOnboardingFlow(
            MerchantOnboardingService onboardingService,
            ImageUploadService imageUploadService) {

        System.out.println("【步骤1】准备商家进件数据");
        System.out.println("----------------------------------------");

        // 创建进件请求
        MerchantOnboardingRequest request = new MerchantOnboardingRequest();

        // 基本信息
        request.setOutBizNo("MERCHANT_" + System.currentTimeMillis());  // 外部业务号，需保证唯一性
        request.setMerchantName("测试商户有限公司");
        request.setMerchantShortName("测试商户");
        request.setMerchantType("ENTERPRISE");  // 企业类型

        // 营业执照信息
        request.setBusinessLicense("91110000MA01234567");  // 统一社会信用代码
        // 注意：实际使用时需要先上传营业执照图片
        // String licenseUrl = imageUploadService.uploadImage(new ImageUploadRequest("BUSINESS_LICENSE", "path/to/license.jpg"));
        // request.setBusinessLicenseUrl(licenseUrl);

        // 法人信息
        request.setLegalPersonName("张三");
        request.setLegalPersonCertType("IDENTITY_CARD");
        request.setLegalPersonCertNo("110101199001011234");
        // 法人证件照片（实际使用时需要上传）
        // request.setLegalPersonCertFrontUrl(imageUploadService.uploadImage(...));
        // request.setLegalPersonCertBackUrl(imageUploadService.uploadImage(...));

        // 联系人信息
        request.setContactName("李四");
        request.setContactMobile("13800138000");
        request.setContactEmail("contact@example.com");

        // 经营地址
        request.setProvince("北京市");
        request.setCity("北京市");
        request.setDistrict("朝阳区");
        request.setAddress("xx路xx号xx大厦xx层");

        // 银行账户信息
        request.setAccountName("测试商户有限公司");
        request.setAccountNo("6212345678901234567");
        request.setBankName("中国工商银行");
        request.setBankBranchName("北京朝阳支行");
        request.setBankProvince("北京市");
        request.setBankCity("北京市");

        // 经营类目
        request.setMccCode("5411");  // 示例：食品零售

        // 店铺信息
        request.setShopName("测试商户门店");
        // request.setShopSignBoardUrl(imageUploadService.uploadImage(...));
        // request.setShopInteriorUrl(imageUploadService.uploadImage(...));

        System.out.println("商户名称：" + request.getMerchantName());
        System.out.println("外部业务号：" + request.getOutBizNo());
        System.out.println();

        System.out.println("【步骤2】提交商家进件申请");
        System.out.println("----------------------------------------");

        // 提交进件申请
        MerchantOnboardingResponse response = onboardingService.submitOnboarding(request);

        if (response.isSuccess()) {
            System.out.println("✓ 进件申请提交成功！");
            System.out.println("申请单号：" + response.getApplyId());
            System.out.println("外部业务号：" + response.getOutBizNo());
            System.out.println();

            // 查询进件状态
            System.out.println("【步骤3】查询进件审核状态");
            System.out.println("----------------------------------------");

            QueryOnboardingStatusRequest queryRequest = new QueryOnboardingStatusRequest(response.getApplyId());
            MerchantOnboardingResponse statusResponse = onboardingService.queryOnboardingStatus(queryRequest);

            if (statusResponse.isSuccess()) {
                System.out.println("✓ 查询成功！");
                System.out.println("审核状态：" + statusResponse.getAuditStatus());
                if (statusResponse.getMerchantPid() != null) {
                    System.out.println("商户PID：" + statusResponse.getMerchantPid());
                }
            } else {
                System.out.println("✗ 查询失败：" + statusResponse.getSubMsg());
            }

        } else {
            System.out.println("✗ 进件申请提交失败！");
            System.out.println("错误码：" + response.getSubCode());
            System.out.println("错误信息：" + response.getSubMsg());
        }

        System.out.println("\n=== 演示完成 ===");
    }
}
